package com.stek.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class SharePreferenceUtils {
	/**
	 * Put String value into share preference
	 * 
	 * @param key
	 *            : The name of the preference to modify.
	 * @param value
	 *            : The name of the preference to modify.
	 * @return: true if successful, otherwise false
	 */
	public static boolean putPrefString(Context context, String key,
			String value) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = sharedPreferences.edit();
		editor.putString(key, value);
		return editor.commit();
	}

	/**
	 * Put integer value into share preference
	 * 
	 * @param key
	 *            : The name of the preference to modify.
	 * @param value
	 *            : The name of the preference to modify.
	 * @return: true if successful, otherwise false
	 */
	public static boolean putPrefInt(Context context, String key, int value) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = sharedPreferences.edit();
		editor.putInt(key, value);
		return editor.commit();
	}

	/**
	 * Put long value into share preference
	 * 
	 * @param key
	 *            : The name of the preference to modify.
	 * @param value
	 *            : The name of the preference to modify.
	 * @return: true if successful, otherwise false
	 */
	public static boolean putPrefLong(Context context, String key, long value) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = sharedPreferences.edit();
		editor.putLong(key, value);
		return editor.commit();
	}

	/**
	 * Put boolean value into share preference
	 * 
	 * @param key
	 *            : The name of the preference to modify.
	 * @param value
	 *            : The name of the preference to modify.
	 * @return: true if successful, otherwise false
	 */
	public static boolean putPrefBoolean(Context context, String key,
			boolean value) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = sharedPreferences.edit();
		editor.putBoolean(key, value);
		return editor.commit();
	}

	/**
	 * @param key
	 *            : The name of the preference to retrieve
	 * @param defaultValue
	 *            : value will be returned if preference does not exist
	 * @return : the preference if it exists otherwise defaultValue
	 */
	public static String getPrefString(Context context, String key,
			String defaultValue) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sharedPreferences.getString(key, defaultValue);
	}

	/**
	 * @param key
	 *            : The name of the preference to retrieve
	 * @param defaultValue
	 *            : value will be returned if preference does not exist
	 * @return : the preference if it exists otherwise defaultValue
	 */
	public static int getPrefInt(Context context, String key, int defaultValue) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sharedPreferences.getInt(key, defaultValue);
	}

	/**
	 * @param key
	 *            : The name of the preference to retrieve
	 * @param defaultValue
	 *            : value will be returned if preference does not exist
	 * @return : the preference if it exists otherwise defaultValue
	 */
	public static long getPrefLong(Context context, String key,
			long defaultValue) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sharedPreferences.getLong(key, defaultValue);
	}

	/**
	 * @param key
	 *            : The name of the preference to retrieve
	 * @param defaultValue
	 *            : value will be returned if preference does not exist
	 * @return : the preference if it exists otherwise defaultValue
	 */
	public static boolean getPrefBoolean(Context context, String key,
			boolean defaultValue) {
		SharedPreferences sharedPreferences = PreferenceManager
				.getDefaultSharedPreferences(context);
		return sharedPreferences.getBoolean(key, defaultValue);
	}
}
